<template>
  <div class="myhead">
    <div class="head">
      <div class="h-left">
        <p>
          请上传JPG、PNG格式的图片,上传成功后如果图片不更新,你可以用浏览器多刷新几次。
        </p>
        <p>上传头像</p>
        <div class="form">
          <div>
            <input @change="selectHead" type="file" />
          </div>
          <div>
            <button @click="upLoad">上传头像</button>
          </div>
        </div>
      </div>
      <div class="h-right">
        <p>您当前的头像:</p>
        <div :class="['head-img', userHeadUrl == null ? '' : 'active']">
          <span v-if="userHeadUrl == null ? true : false">预览</span>
          <div class="img-box">
            <img :src="userHeadUrl" alt="" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  name: "userHead",
  data() {
    return {
      userHeadUrl: null,
    };
  },
  methods: {
    selectHead(event) {
      let imgInfo = event.target.files[0];
      let fileReader = new FileReader();

      fileReader.onload = (data) => {
        let urlStr = data.target.result;
        this.userHeadUrl = urlStr;
        // console.log(userHeadUrl);
      };

      fileReader.readAsDataURL(imgInfo);
    },
    upLoad() {
      if(this.userHeadUrl != null){
        this.$store.state.headUrl = this.userHeadUrl;
        alert("上传成功");
        this.$store.state.isImgUrl = true;
        this.$router.push({path:"/home/myhomepage"})
      }else {
        alert("请选择一张图片")
      }
    },
  },
  computed: {
    ...mapState(["headUrl"]),
  },
};
</script>

<style lang="less" scoped>
.myhead {
  width: 100%;
  .head {
    width: 100%;
    display: flex;
    justify-content: space-between;
    .h-left {
      width: 350px;
      padding-top: 25px;
      p {
        color: #808080;
        line-height: 22px;
        width: 220px;
        &:nth-of-type(2) {
          color: #000;
        }
      }
      .form {
        input[type="file"] {
          padding: 2px 5px;
          margin: 10px 0 30px 0;
          font-size: 16px;
        }
        button {
          border: 1px solid #ccc;
          font-size: 16px;
          padding: 5px 15px;
          color: #808080;
          cursor: pointer;
          &:active {
            color: #000;
          }
        }
      }
    }
    .h-right {
      flex-grow: 1;
      p {
        color: #808080;
        font-size: 18px;
        margin: 20px 0;
      }
      .head-img {
        width: 270px;
        height: 270px;
        line-height: 270px;
        // margin: 0 auto;
        color: #808080;
        border: 1px dashed #ccc;
        text-align: center;
        position: relative;
        span {
          position: absolute;
          left: 50%;
          top: 50%;
          display: block;
          width: 50px;
          height: 50px;
          margin-left: -25px;
          top: 0;
        }
        &.active {
          border: 1px solid transparent;
          .img-box {
            width: 100%;
            height: 100%;
            padding: 2px 2px;
            box-sizing: border-box;
            box-shadow: 2px 2px 2px 2px #ccc;
            position: absolute;
            left: 0;
            top: 0;
            img {
              width: 100%;
              height: 100%;
            }
          }
        }
      }
    }
  }
}
</style>